package one.stand.model;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import one.stand.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * 返回数据类
 *
 * @author Administrator
 * @param <T>
 */

@ApiModel(value = "公共模块")
public class ResultModel<T> implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(ResultModel.class);
    @ApiModelProperty(value = "标识码", name = "code")
    private Integer code;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty(value = "成功状态")
    private boolean isSuccess;
    @ApiModelProperty("返回信息")
    private String message;

    public ResultModel() {
    }

    public ResultModel(ResultEnum resultEnum) {
        if (resultEnum.getCode().equals(ResultEnum.SUCCESS.getCode())) {
            isSuccess = true;
        } else {
            isSuccess = false;
        }
        code = resultEnum.getCode();
        message = resultEnum.getMessage();
    }

    public ResultModel(T t) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.message = ResultEnum.SUCCESS.getMessage();
        this.data = t;
    }

    public ResultModel(String messages) {
        code = ResultEnum.FAIL.getCode();
        message = messages;
    }

    /**
     * 参数缺失
     * @return
     */
    public static ResultModel checkParamFail() {
        return new ResultModel(ResultEnum.NO_PARAM);
    }

    /**
     * 操作失败
     *
     * @return
     */
    public static ResultModel error() {
        return new ResultModel(ResultEnum.ERROR);
    }

    /**
     * 自定义异常消息
     */
    public static ResultModel fail(String message) {
        return new ResultModel(message);
    }

    /**
     * 账号过期
     * @return
     */
    public static ResultModel account(){
        return new ResultModel(ResultEnum.ACCOUNT);
    }
    static ResultModel account(String message){
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(ResultEnum.ACCOUNT.getCode());
        resultModel.setMessage(message);
        return resultModel;
    }
    /**
     * 操作失败 默认201
     *
     * @return
     */
    public static ResultModel fail() {
        return new ResultModel(ResultEnum.FAIL);
    }

    public static ResultModel faultTolerant() {
        return new ResultModel(ResultEnum.FAULT_TOLERANT);
    }

    /**
     * 无数据
     */
    public static ResultModel noData() {
        return new ResultModel(ResultEnum.NO_DATA);
    }

    /**
     * 操作成功带参数
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> ResultModel success(T t) {
        return new ResultModel(t);
    }

    /**
     * 操作成功不带参数
     *
     * @return
     */
    public static ResultModel success() {
        return new ResultModel(ResultEnum.SUCCESS);
    }

    /**
     * 手动返回
     *
     * @param response
     * @param resultEnum
     * @throws Exception
     */
    public static void returnJson(HttpServletResponse response, ResultEnum resultEnum) throws Exception {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(JSON.toJSON(fail(resultEnum)));
        } catch (IOException e) {
            log.error("response error", e);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * 操作失败
     *
     * @return
     */
    public static ResultModel fail(ResultEnum resultEnum) {
        return new ResultModel(resultEnum);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        if(code==null){
            return false;
        }
        if(code.equals(ResultEnum.SUCCESS.getCode())){
            return true;
        }
        return false;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }

    public void setT(T t) {
        this.data = t;
    }
}
